<?php
namespace modules\borrow;

use modules\borrow\model\Borrow as BorrowModel;
use modules\book\model\Book as BookModel;
class Borrow {
  protected $borrow;
  protected $book;
  public function __construct(BorrowModel $borrow, BookModel $book) {
    $this->borrow = $borrow;
    $this->book = $book;
  }

  public function data() {
    $data = $this->borrow->where('admin_id',1000)->get();
    return empty($data) ? [] : $data;
  }

  public function selfData($res) {
    return $this->borrow->where('user_id', $res['user_id'])->get();
  }

  // 用户提交借阅表单, 等待管理员审核
  public function submit($res) {
    // var_dump($res);
    return $this->borrow->insert($res);
  }

  // 借阅
  public function borrow($res) {
    // 修改借阅数据中的 admin_id
    $this->borrow->where('borrow_id', $res['borrow_id'])->update(['admin_id' => $res['admin_id']]);
    // 获取对应 book_id 的 num值
    $num = ($this->book->where('book_id', $res['book_id'])->get())[0]['num'];
    // 将对应 book_id 的 num 值修改为原来的 num-1 
    return $this->book->where('book_id', $res['book_id'])->update(['num' => $num-1]);
  }
}
?>